// 2025/4/6
// https://leetcode.cn/problems/merge-two-sorted-lists/description/?envType=study-plan-v2&envId=top-100-liked
// 合并两个有序链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
    public:
        ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
            ListNode head, *cur = &head;
            while(list1 && list2)
            {
                if(list1->val < list2->val)
                {
                    cur->next = list1;
                    cur = cur->next;
                    list1 = list1->next;
                }
                else
                {
                    cur->next = list2;
                    cur = cur->next;
                    list2 = list2->next;
                }
            }
            cur->next = list1 ? list1 : list2;
            return head.next;
        }
    };